package com.fengzai.demo.config;

import com.fengzai.netty.common.callback.HandlerProcessor;
import com.fengzai.netty.common.domain.TcpMsgInfo;
import com.fengzai.netty.tcp.DefaultTcpClient;
import com.fengzai.netty.tcp.DefaultTcpServer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @PACKAGE_NAME: com.fengzai.demo.config
 * @author: rhf
 * @ProjectName: netty
 * @DATE: 2021/10/18
 **/
@Configuration
public class TcpConfiguration {
    /**
     * 服务端ip
     */
    private Integer servicePort = 10003;

    @Bean(destroyMethod = "stop")
    public DefaultTcpServer tcpServer() {
        HandlerProcessor<TcpMsgInfo<String>, Object> handlerProcessor = new HandlerProcessor<TcpMsgInfo<String>, Object>() {
            @Override
            public Object process(TcpMsgInfo<String> msg) {
                return null;
            }
        };
        DefaultTcpServer tcpServer = new DefaultTcpServer(handlerProcessor);
        new Thread() {
            @Override
            public void run() {
                tcpServer.run(servicePort);
            }
        }.start();
        return tcpServer;
    }

    @Bean(destroyMethod = "stop")
    public DefaultTcpClient tcpClient() {
        HandlerProcessor<TcpMsgInfo, ? super Object> handlerProcessor = (HandlerProcessor<TcpMsgInfo, Object>) msgInfo -> null;
        DefaultTcpClient tcpClient = new DefaultTcpClient("127.0.0.1", servicePort);
        new Thread() {
            @Override
            public void run() {
                tcpClient.run(null);
            }
        }.start();
        return tcpClient;
    }
}
